use std::mem;

fn main() {
    println!("数组与切片");

    let xs = [1, 2, 3, 4, 5];
    println!("定长数组 xs: {:?}", xs); // 长数组 xs: [1, 2, 3, 4, 5]

    let xs = [1; 8];
    println!("定义n个相同元素 [1; 8]: {:?}", xs);

    let xs = [1, 2, 3, 4, 5, 6, 7, 8];
    println!("打印指定元素,从0开始: {}", xs[2]);

    println!("数组大小 xs.len(): {}", xs.len());

    let xs = [1, 2, 3, 4, 5];
    println!(
        "数组在栈中分配的内存([1, 2, 3, 4, 5]): {} bytes",
        mem::size_of_val(&xs)
    );

    println!("代用数组一部分,成为(&xs[2..]) slice: {:?}", &xs[2..]);

    println!("下标越界(xs[5]) panic: {:?}", xs[5]); // index out of bounds: the length is 5 but the index is 5
}
